package com.ztgf.logistics.service.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.constant.SysConstant;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.vo.UserVO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillDTO;
import com.ztgf.logistics.models.dto.deppon.LogisticsUserDTO;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillService;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserInfoService;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserService;
import com.ztgf.logistics.models.vo.deppon.LogisticsUserAuditVO;
import com.ztgf.logistics.models.vo.deppon.LogisticsUserVO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.audit.AuditDeBangDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.audit.IAuditDebangService;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.sys.models.service.ISessionService;
import com.ztgf.sys.models.service.ISysConfigService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

/**
 * @Description:
 * @Author: mxli
 * @Date: 2021-10-16 17:11
 **/
@DubboService
public class LogisticsUserInfoServiceImpl implements ILogisticsUserInfoService {

    @DubboReference
    private IShopInfoService shopInfoService;
    @DubboReference
    private ISysConfigService sysConfigService;
    @DubboReference
    private ISessionService sessionService;
    @DubboReference
    private ILogisticsUserService logisticsUserService;
    @DubboReference
    private IAuditService auditService;
    @DubboReference
    private ILogisticsBillService logisticsBillService;
    @DubboReference
    private IAuditDebangService auditDebangService;

    @Override
    public Result<LogisticsUserAuditVO> get(Long shopId) {
        ShopInfoDTO shopInfoDTO = shopInfoService.selectById(shopId);
        if(shopInfoDTO == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        LogisticsUserDTO dto = logisticsUserService.selectById(shopId);
        if(dto == null){
            UserVO userVO = sessionService.getUserVO(CommonParameter.getHeadParamsBO().getSessionId(), CommonParameter.getHeadParamsBO().getSource().toString());
            dto = new LogisticsUserDTO();
            dto.setId(shopId);
            dto.setCreateUserId(userVO.getId());
            dto.setCreateUserName(userVO.getName());
            Long selfShopId = sysConfigService.getLongByCode(SysConstant.SELF_OPERATED_SHOP_ID);
            if(shopId.equals(selfShopId)){
                dto.setType(2);
                dto.setContractStatus(1);
            }
            logisticsUserService.insert(dto);
        }
        LogisticsUserAuditVO vo = new LogisticsUserAuditVO();
        BeanUtils.copyProperties(dto, vo);
        AuditDeBangDTO auditDeBangDTO = auditDebangService.selectById(dto.getAuditId());
        if (auditDeBangDTO!=null){
            vo.setFinanceName(auditDeBangDTO.getFinanceName());
            vo.setFinancePhone(auditDeBangDTO.getFinancePhone());
            vo.setFinanceWx(auditDeBangDTO.getFinanceWx());
            vo.setFinanceQq(auditDeBangDTO.getFinanceQq());
            vo.setSendName(auditDeBangDTO.getSendName());
            vo.setSendPhone(auditDeBangDTO.getSendPhone());
            vo.setSendWx(auditDeBangDTO.getSendWx());
            vo.setSendQq(auditDeBangDTO.getSendQq());
        }
        //审核状态 0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        //审核状态：0未申请、1审核中、2审核成功、3审核不通过
        Integer auditStatus = 0;
        String auditContent = null;
        AuditDTO auditDTO = auditService.selectById(dto.getAuditId());
        if(auditDTO != null){
            switch (auditDTO.getStatus()){
                case 0:
                    auditStatus = 1;
                    break;
                case 1:
                    auditStatus = 2;
                    break;
                case 3:
                    auditStatus = 3;
                    break;
            }
            auditContent = auditDTO.getContent();
        }
        vo.setAuditStatus(auditStatus);
        vo.setAuditContent(auditContent);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("shop_id", shopId);
        wrapper.orderBy("id", false);
        wrapper.last("limit 1");
        LogisticsBillDTO billDTO = logisticsBillService.selectOne(wrapper);
        if(billDTO != null){
            vo.setBillMoneyStatus(billDTO.getPayStatus());
            vo.setBillOverdueStatus(billDTO.getOverdueStatus());
            vo.setBillPayTime(billDTO.getPayTime());
        }
        return new Result<>(vo);
    }
}
